package com.example.egmvc1.basic.exception;

import com.example.egmvc1.basic.response.JsonResult;
import com.example.egmvc1.basic.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult handleException(RuntimeException e) {

        String data = e.getMessage();
        log.error("RuntimeException={}", data);
        return new JsonResult(StatusCode.FAIL, data);
    }
    @ExceptionHandler
    public JsonResult handleException(IllegalArgumentException e) {

        String data = e.getMessage();
        log.error("IllegalArgumentException={}", data);
        return new JsonResult(StatusCode.PARAM_ERROR, data);
    }
    @ExceptionHandler
    public JsonResult handleBindException(BindException e) {

        String data = e.getFieldError().getDefaultMessage();
        log.error("BindException={}", data);
        return new JsonResult(StatusCode.UNKNOWN_ERROR, data);
    }
    @ExceptionHandler
    public JsonResult handleException(Throwable e) {

        String data = e.getMessage();
        log.error("Throwable={}", data);
        log.error("Throwable={}", e);
        return new JsonResult(StatusCode.UNKNOWN_ERROR, data);
    }

}
